package com.dzy.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dzy.base.BaseController;
import com.dzy.entity.Admin;
import com.dzy.service.AdminService;
import com.dzy.service.RoleService;
import com.dzy.util.QiniuUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @Reference
    private AdminService adminService;
    @Reference
    private RoleService roleService;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @RequestMapping
    public String findPage(HttpServletRequest request, Model model) {
        Map<String, Object> filters = super.getFilters(request);
        PageInfo<Admin> page = this.adminService.findPage(filters);
        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        return "admin/index";
    }

    @GetMapping("/create")
    public String create() {
        return "admin/create";
    }

    @PostMapping("/save")
    public String save(Admin admin) {
        admin.setHeadUrl("http://47.93.148.192:8080/group1/M00/03/F0/rBHu8mHqbpSAU0jVAAAgiJmKg0o148.jpg");
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        this.adminService.insert(admin);
        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        Admin admin = this.adminService.getById(id);
        model.addAttribute("admin", admin);
        return "admin/edit";
    }

    @PostMapping("/update")
    public String update(Admin admin) {
        this.adminService.update(admin);
        return "common/successPage";
    }

    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        this.adminService.delete(id);
        return "redirect:/admin";
    }

    @RequestMapping("/uploadShow/{id}")
    public String uploadShow(@PathVariable Long id, Model model) {
        model.addAttribute("id", id);
        return "admin/upload";
    }

    @RequestMapping("/upload/{id}")
    public String upload(@PathVariable Long id, MultipartFile file) throws IOException {
        //上传头像到七牛云
        String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String fileName = UUID.randomUUID().toString();
        fileName += extName;
        QiniuUtil.upload2Qiniu(file.getBytes(), fileName);
        //写数据到数据库中
        Admin admin = new Admin();
        admin.setId(id);
        admin.setHeadUrl("http://sdu5w52t1.hb-bkt.clouddn.com/" + fileName);
        adminService.update(admin);
        return "common/successPage";
    }

    /**
     * 进入分配角色页面
     *
     * @param adminId
     * @return
     */
    @GetMapping("/assignShow/{adminId}")
    public String assignShow(ModelMap model, @PathVariable Long adminId) {
        Map<String, Object> roleMap = roleService.findRoleByAdminId(adminId);
        model.addAllAttributes(roleMap);
        model.addAttribute("adminId", adminId);

        return "admin/assignShow";
    }

    /*
    页面："1,4,6"
    List<Long> roleIds:["1","4","6",null]
     */
    @RequestMapping("/assignRole")
    public String assignRole(Long adminId, Long[] roleIds) {
        this.roleService.saveUserRoleRealtionShip(adminId, roleIds);
        return "common/successPage";
    }
    /**
     * 获取当前登录信息
     * @return
     */
    @GetMapping("/getInfo")
    @ResponseBody
    public Object getInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();//登录用户的信息，

        return authentication.getPrincipal();
    }

}
